import java.util.Arrays;
import java.util.Scanner;

public class tets {
    public static  int binarySearch(int[]array,int key){//二分查找的方法。这个方法只能使用于从小到大的有序数组
        int left=0;
        int right=array.length;
        while (left<=right){
            int mid=(left+right)/2;
            if(array[mid]>key){
                right=mid;
            } else if (array[mid]==key) {
                return mid;
            }else {
                left=mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        //使用冒泡排序练习
        int[] array={1,4,5,2,9,7,8};//一个无序数组
        for (int i = 0; i < array.length-1; i++) {
            for(int j=0;j<array.length-1;j++){
                if(array[j]>array[j+1]){
                  int c=array[j];
                  array[j]=array[j+1];
                  array[j+1]=c;
                }
            }
        }
        for (int x:array) {
            System.out.print(x+" ");

        }
    }
    public static void main1(String[] args) {
        //实现二分查找
        int[] array={1,2,3,4,5,6};
        Scanner scanner=new Scanner(System.in);
        int key=scanner.nextInt();
        int num=binarySearch(array,key);
        if(num==-1){
            System.out.println("没找到");
        }else System.out.println("在"+num);
    }
}
